package net.wanho.personalOffice.dao.impl;

import net.wanho.commons.dao.impl.BaseDAOImpl;
import net.wanho.commons.exception.DAOException;
import net.wanho.commons.exception.DataAccessException;
import net.wanho.personalOffice.dao.TaskUserRelationsDAO;
import net.wanho.personalOffice.po.TaskUserRelations;

import java.util.List;

public class TaskUserRelationsDAOImpl extends BaseDAOImpl<TaskUserRelations> implements TaskUserRelationsDAO {

    @Override
    public boolean insertBatch(List<TaskUserRelations> taskUserRelationsList) throws DAOException {
        try {
            String sql ="INSERT INTO task_user_relations\n" +
                    "            (TASK_ID,\n" +
                    "             EMPLOYEE_ID)\n" +
                    "VALUES (?,?);";
            Object[][] params = new Object[taskUserRelationsList.size()][2];
            for (int i = 0; i < taskUserRelationsList.size(); i++) {
                TaskUserRelations taskUserRelations = taskUserRelationsList.get(i);
                params[i][0] = taskUserRelations.getTaskId();
                params[i][1] = taskUserRelations.getEmployeeId();
            }
            return super.jdbcTemplate.updateBatch(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public boolean delete(String id) throws DAOException {
        try {
            String sql ="DELETE\n" +
                    "FROM task_user_relations\n" +
                    "WHERE TASK_ID = ?;";
            Object[] params = {id};
            return super.jdbcTemplate.delete(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }
}
